//index.js
const utils = require("../../utils/utils.js");
const biz = require("../../utils/bizUtils.js");
const listUtil = require("../../utils/listUtil.js");
const session = require("../../utils/session.js");
const toast = require("../../utils/toasts.js");
const wxm = require("../../utils/wechat_modules.js");
const com = require("../../utils/commonRequest.js");

import {
  cus_change_status,
  cus_tags,
  agent_verify
} from "../../utils/api.js";

//获取应用实例
const app = getApp()
const globalData = app.globalData;

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    hidden: true,
    scrollTop: 0,
    scrollHeight: 0,
    title: '',
    keyword: null,
    catId: null,
    orderRule: 'HOT',
    waiting: true,
    share_modal: false,
    noMore: false,
    banners: ['https://img.lingxing.site/jx/small_banner1_1.png'],
    isIphoneX: globalData.isIphoneX,
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function(options) {
    var that = this;
    toast.showLoading();
    wx.getSystemInfo({
      success: function(res) {
        console.info(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });

    if (options) {
      if (options.keyword) {
        this.setData({
          keyword: options.keyword
        });
      }

      if (options.from) {
        this.setData({
          from: options.from
        })
      }


      if (options.salesId) {
        this.setData({
          salesId: options.salesId
        })
        if (options.salesName) {}
      }

      if (options.index) {
        this.setData({
          index: options.index
        });
      }

      if (options.type) {
        this.setData({
          type: options.type,
        })
      }
    }
    this.getUserInfo();

    wxm.getLocation(this, that.getData);
  },
  getData: function() {
    if (!this.data.lat) {
      wxm.showModal({
        title: '无法获取定位',
        content: '您可以在小程序设置中打开授权或者手动切换地址',
        hideCancel: true,
        isIt: (true)
      }).then(() => {}).catch(() => {});
      this.setData({
        lat: 30.25961,
        lng: 120.13026
      })
    }
    this.isAuth();
    com.locToAddress(this.data.lat, this.data.lng, this, this.refresh);
  },
  onShow: function() {},
  onShareAppMessage: function(res) {
    if (this.data.isAgent && this.data.agentIndex != null) {
      var list = this.data.list;
      var obj = list[this.data.agentIndex];
      var url = '/pages/agentAdd/agentAdd?topId=' + obj.topSellerId + '&upId=' + obj.sellerId;

      return wxm.baseShare(this, '欢迎代理' + obj.company + '的商品', url);
    }

    return wxm.commonShare(this);
  },
  getList() {
    console.log("---" + this.data.location);
    var type = this.data.type;

    var queryParam = {
      type: 'JX'
    };
    queryParam.lat = this.data.lat;
    queryParam.lng = this.data.lng;
    queryParam.orderRule = 'DIS';

    var loc = this.data.loc;
    if (loc.city) {
      queryParam.city = loc.city;
    }

    var user = session.getUserSync();

    var api = "/seller/query";

    var that = this;

    listUtil.getList(this, api, queryParam, function(item) {
      // console.log(item);
      item.addressStr = biz.addressStr(item);
      item.rateScore = biz.rateScore(item.totalRateScore, item.totalRateCount);
      item.rates = biz.rates(item.rateScore);
      item.distanceStr = '距您' + biz.disStr(item.distance);
    });
  },
  bindDownLoad: function() {
    getList();
  },
  scroll: function(event) {
    listUtil.scroll(this, event);
  },
  refresh: function(event) {
    listUtil.refresh(this);
  },
  onPullDownRefresh: function() {
    listUtil.onPullDownRefresh(this);
  },
  onReachBottom: function() {
    listUtil.onReachBottom(this);
  },
  // 获取当前用户信息
  getUserInfo: function() {
    session.getUser(this);
  },
  pushFormSubmit: function(e) {
    console.log(e);
    console.log("formId:" + e.detail.formId);
    com.saveFormId(e);
  },
  selectMap: function() {
    var that = this;
    var loc = this.data.loc;
    wx.chooseLocation({
      name: loc.address,
      latitude: that.data.lat,
      longitude: that.data.lng,
      success: function(res) {
        console.log(res);
        loc.address = res.address;
        if (!loc.address) {
          return;
        }
        var map = biz.parseAddress(loc.address);
        if (map.province) {
          loc.province = map.province;
        }
        if (map.city) {
          loc.city = map.city;
        }
        console.log(map);
        that.setData({
          position: res,
          loc,
          address: res.name,
          lat: res.latitude,
          lng: res.longitude,
        });
        that.refresh();
      },
    })
  },
  isAuth: function() {
    var that = this;
    wxm.auth('scope.userLocation', function() {
      that.setData({
        isAuth: true
      })
    });
  },
  bindopensetting: function() {
    wxm.getLocation(this, this.getData);
  }
})